package cn.ondraw.ragflow.service.impl;

import cn.hutool.json.JSONUtil;
import cn.ondraw.ragflow.pojo.request.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.request.*;
import cn.ondraw.ragflow.pojo.response.KbCreateResponse;
import cn.ondraw.ragflow.service.RagFlowKnowledgeBaseService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class RagFlowKnowledgeBaseServiceImpl implements RagFlowKnowledgeBaseService {

    private final PythonApiCaller pythonApiCaller;

    private final String URL_PREFIX = "/v1/kb";

    /**
     * 创建知识库
     *
     * @param request 创建知识库的请求对象
     * @return 创建知识库的响应对象
     */
    @Override
    public KbCreateResponse create(KbCreateRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/create", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, KbCreateResponse.class, true);
    }

    /**
     * 更新知识库
     *
     * @param request 更新知识库的请求对象
     * @return 包含更新结果的映射
     */
    @Override
    public Map<String, Object> update(KbUpdateRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/update", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, Map.class, true);
    }

    /**
     * 获取知识库详情
     *
     * @return 包含知识库详情的映射
     */
    @Override
    public Map<String, Object> detail(String kbId) {
        String url = URL_PREFIX + "/detail?kb_id=" + kbId;
        ResponseEntity<?> response = pythonApiCaller.callGetApi(url, false);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, Map.class, true);
    }

    /**
     * 列出知识库列表
     *
     * @param request 列出知识库列表的请求对象
     * @return 包含知识库列表的映射
     */
    @Override
    public Map<String, Object> list(KbListRequest request) {
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/list", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, Map.class, true);
    }

    /**
     * 删除知识库
     *
     * @param request 删除知识库的请求对象
     * @return 包含删除结果的映射
     */
    @Override
    public Map<String, Object> rm(KbRmRequest request) {
        // 注意：PythonApiCaller 目前没有 DELETE 方法，可能需要扩展
        // 这里暂时使用 POST 代替，实际需要调整
        ResponseEntity<?> response = pythonApiCaller.callPostApi(URL_PREFIX + "/rm", request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, Map.class, true);
    }

    /**
     * 列出指定知识库的标签
     *
     * @param kbId 知识库的 ID
     * @return 包含标签列表的映射
     */
    @Override
    public Map<String, Object> listTags(String kbId) {
        String url = URL_PREFIX + "/" + kbId + "/tags";
        ResponseEntity<?> response = pythonApiCaller.callGetApi(url);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, Map.class, true);
    }

    /**
     * 从多个知识库中列出标签
     *
     * @param request 从多个知识库中列出标签的请求对象
     * @return 包含标签列表的映射
     */
    @Override
    public Map<String, Object> listTagsFromKbs(KbTagsRequest request) {
        String url = URL_PREFIX + "/tags?kb_ids=" + String.join(",", request.getKbIds());
        ResponseEntity<?> response = pythonApiCaller.callGetApi(url);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, Map.class, true);
    }

    /**
     * 删除指定知识库的标签
     *
     * @param request 删除指定知识库标签的请求对象
     * @return 包含删除结果的映射
     */
    @Override
    public Map<String, Object> rmTags(KbRmTagsRequest request) {
        String url = URL_PREFIX + "/" + request.getKbId() + "/rm_tags";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(url, request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, Map.class, true);
    }

    /**
     * 重命名指定知识库的标签
     *
     * @param request 重命名指定知识库标签的请求对象
     * @return 包含重命名结果的映射
     */
    @Override
    public Map<String, Object> renameTags(KbRenameTagRequest request) {
        String url = URL_PREFIX + "/" + request.getKbId() + "/rename_tag";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(url, request);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, Map.class, true);
    }

    /**
     * 获取指定知识库的知识图谱
     *
     * @param kbId 知识库的 ID
     * @return 包含知识图谱信息的映射
     */
    @Override
    public Map<String, Object> knowledgeGraph(String kbId) {
        String url = URL_PREFIX + "/" + kbId + "/knowledge_graph";
        ResponseEntity<?> response = pythonApiCaller.callGetApi(url);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, Map.class, true);
    }

    /**
     * 删除指定知识库的知识图谱
     *
     * @param kbId 知识库的 ID
     * @return 包含删除结果的映射
     */
    @Override
    public Map<String, Object> deleteKnowledgeGraph(String kbId) {
        // 注意：PythonApiCaller 目前没有 DELETE 方法，可能需要扩展
        // 这里暂时使用 POST 代替，实际需要调整
        String url = URL_PREFIX + "/" + kbId + "/knowledge_graph";
        ResponseEntity<?> response = pythonApiCaller.callPostApi(url, null);
        return JSONUtil.toBean(response.getBody() != null ? response.getBody().toString() : null, Map.class, true);
    }
}
